// pages/registerinfo/registerinfo.js
import areaList from '../../utils/area'
import {saveAndWxAccount} from '../../api/index'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    areaShow: false,
    areaList: [],
    fileList: [],
    formData: {
      nickname: '',
      address: '',
      addressText: '',
      detailAddress: '',
      type: 'CAROWNER'
    }
  },

  afterRead(event) {
    let _this = this
    wx.showLoading({
      title: '加载中',
    })
    const {file} = event.detail;
    wx.uploadFile({
      url: `${getApp().globalData.baseUrl}/sys/attachment/upload`,
      filePath: file.url,
      name: 'file',
      success(res) {
        let {data, success} = JSON.parse(res.data)
        if (success) {
          _this.setData({
            'formData.headImg': data.attachment,
            fileList: [{url: data.attachment}]
          })
        }
        wx.hideLoading()
      }
    });
  },
  deleteImg(e) {
    this.setData({
      'formData.headImg': '',
      fileList: []
    })
  },
  selectArea() {
    this.setData({
      areaShow: true
    })
  },
  areaConfirm(val) {
    if (!val.detail.index.includes(0)) {
      const addressText = `${val.detail.values[0].name}${val.detail.values[1].name}${val.detail.values[2].name}`
      const addressCode = `${val.detail.values[0].code},${val.detail.values[1].code},${val.detail.values[2].code}`
      this.setData({
        areaShow: false,
        'formData.addressText': addressText,
        'formData.address': addressCode
      })
    }
  },
  areacancel(){
    this.setData({
      areaShow: false
    })
  },
  bindKeyInput: function (e) {
    const formDataKey = `formData.${e.target.dataset.key}`
    this.setData({
      [formDataKey]: e.detail.value
    })
  },
  toRegisterInfo() {
    if (!this.data.formData.nickname) {
      wx.showToast({
        title: "姓名不能为空",
        icon: 'none',
        duration: 1500
      })
      return
    } else if (!this.data.formData.address) {
      wx.showToast({
        title: "请选择省市区",
        icon: 'none',
        duration: 1500
      })
      return
    } else if (!this.data.formData.headImg) {
      wx.showToast({
        title: "请上传门头照",
        icon: 'none',
        duration: 1500
      })
      return
    }
    const formData = {
      mobile: app.globalData.userInfo.phoneNumber,
      openId: app.globalData.openid,
      ...this.data.formData
    }
    saveAndWxAccount(formData).then(res => {
      let {data, success} = res
      if (success && data.id) {
        app.globalData.userInfo = data
        app.globalData.userId = data.id
        app.globalData.phone = data.mobile
        app.globalData.token = data.token
        wx.navigateTo({
          url: `/pages/home/home`
        })
      }
    })
    console.log(formData)
    //console.log(e.target.dataset.url);
    // wx.navigateTo({
    //   url:e.target.dataset.url
    // })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      areaList: areaList
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
